package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.ResultEntity;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.UserService;
import common.ReturnInfoEnum;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName:UserController
 * @Description:用户信息访问操作
 * @Author:Deamer
 * @Date:2020/3/30 17:44
 **/
@RestController
public class UserController {
    @Resource
    UserService userService;

    /***
     * 实现用户在连续登录错误5次之后，直接锁定一个小时的功能。
     * @param map
     * @return
     */
    @RequestMapping("/login")
    public ResponseEntity<?> userLogin(@RequestBody Map<String, Object> map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        //通过username查询用户的信息
        Boolean boo = userService.getUsername(username);
        if (boo == false) {//用户不存在
            return ResponseEntity.ok().body(new ResultEntity(ReturnInfoEnum.REQUEST_FAIL.getCode(), "用户不存在！", null));
        }
        //登录验证：超过五次直接锁定一个小时
        JSONObject jsonObject = userService.getUserInfo(username, password);
        if ("1001".equals(jsonObject.get("code"))) {//用户验证不成功
            return ResponseEntity.ok().body(new ResultEntity(ReturnInfoEnum.REQUEST_FAIL.getCode(), jsonObject.getString("msg"), null));
        }
        UserInfo userInfo = (UserInfo) jsonObject.get("data");
        Date thisErrorTime = null;//修改本次错误登录时间
        Integer isLocked = 0;//获取是否锁定的状态 0：未锁定 1：锁定
        /*******************(1)用户名、密码验证成功*************************/
        //锁定标志为0
        if (userInfo.getLoginLockFlag() == 0) {//账号没有锁定
            //锁定状态为0
            userInfo.setLoginLockFlag(0);
            //错误计数为0
            userInfo.setLoginErrorCount(0);

            isLocked = userInfo.getLoginLockFlag();

        }


        return ResponseEntity.ok().body(new ResultEntity(ReturnInfoEnum.SUCCESS.getCode(), ReturnInfoEnum.SUCCESS.getMessage(), null));
    }
}
